package com.jse.service;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.List;

import javax.script.ScriptException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Streams;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;

import com.jse.plug.JseSession;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

/**
 * NashornRun表达引擎执行类 mvc代理服务组件
 * 
 * @author dzh
 * 
 */
@IocBean
public class NashornService {

	Log log = Logs.get();
	@Inject
	Dao dao;
	@Inject
	EhcacheService cache;
	@Inject
	NashornScriptEngine nashornScriptEngine;
	@Inject("conf")
	PropertiesProxy conf;

	// @Inject RedisService redisService;//未使用pipeline
	// @Inject JedisPool jedisPool;//原生操作redis

	public Object run(String jjs, String func, NutMap tbl, JseSession session)
			throws ScriptException, NoSuchMethodException {
		String jjs_path = this.getClass().getResource("/").getPath().substring(1);// .replace("/", "\\");
		List<String> pargs = Mvcs.getActionContext().getPathArgs();
		boolean notExists = true;
		if (pargs.size() > 1) {
			String jjsaction = jjs + "\\" + func;
			try {
				jjsaction = jjs + "\\" + func.substring(0, func.indexOf('_'));
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (Files.exists(Paths.get(jjs_path, jjsaction + ".jjs"), LinkOption.NOFOLLOW_LINKS)) {// 直接映射不存在
				jjs = jjsaction;
				notExists = false;
			} else if (func.contains("_")) {
				jjs = jjs + "\\" + func.substring(0, func.indexOf('_'));
				notExists = false;
			} else if (Files.exists(Paths.get(jjs_path, jjs + "/_.jjs"), LinkOption.NOFOLLOW_LINKS)) {
				jjs = jjs + "/_";
				notExists = false;
			}
		} else {
			if (Files.exists(Paths.get(jjs_path, jjs + ".jjs"), LinkOption.NOFOLLOW_LINKS)) {// 直接映射不存在
				notExists = false;
			}
		}
		if (notExists) {
			log.warnf("jjs File %s.jjs not exists!", jjs);
			return null;
		}
		ServletRequest req = Mvcs.getReq();
		ServletResponse resp = Mvcs.getResp();
		nashornScriptEngine.put("req", req);
		nashornScriptEngine.put("resp", resp);
		nashornScriptEngine.put("session", session);
		nashornScriptEngine.put("app", req.getServletContext());
		nashornScriptEngine.put("ioc", Mvcs.getIoc());
		nashornScriptEngine.put("conf", conf);
		nashornScriptEngine.put("dao", dao);
		nashornScriptEngine.put("log", log);
		nashornScriptEngine.put("cache", cache);
		nashornScriptEngine.put("jjs", jjs_path);
		// nashornScriptEngine.eval("load(jjs+\"jse.jjs\");");
		nashornScriptEngine.eval(Streams.fileInr("jse.jjs"));
		nashornScriptEngine.put("p", tbl);
		nashornScriptEngine.eval(Streams.fileInr(jjs_path + jjs + ".jjs"));
		return nashornScriptEngine.invokeFunction(func, tbl);
	}

	public Object run(String jjs, String func, Object tbl) throws ScriptException, NoSuchMethodException {
		String jjs_path = this.getClass().getResource("/").getPath().substring(1);// .replace("/", "\\");
		nashornScriptEngine.put("conf", conf);
		nashornScriptEngine.put("dao", dao);
		nashornScriptEngine.put("log", log);
		nashornScriptEngine.put("cache", cache);
		nashornScriptEngine.put("jjs", jjs_path);
		nashornScriptEngine.put("p", tbl);
		nashornScriptEngine.eval(Streams.fileInr(jjs_path + jjs + ".jjs"));
		return nashornScriptEngine.invokeFunction(func, tbl);
	}

	public static void main(String[] args) throws NoSuchMethodException, ScriptException {
		NashornService service = new NashornService();
		service.nashornScriptEngine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine();
		System.err.println(service.run("test", "test", "123"));
	}
}
